package com.itheima.reggie.exception;

import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.SQLIntegrityConstraintViolationException;

/**
 * @author mwj
 * @date 2021/11/22
 * @TIME:11:41
 */
//全局异常处理器
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    @ResponseBody
    public R ex(SQLIntegrityConstraintViolationException e){

        return R.error("已存在");
    }

//在全局异常处理器中增加方法，用于捕获我们自定义的异常 CustomException
    @ExceptionHandler(CustomException.class)
    @ResponseBody
    public R ex(CustomException e){

        return R.error(e.getMessage());
    }
}
